package com.mpgame.database.mysql.service.game;

import com.mpgame.database.mysql.mapper.game.PlayersMapper;
import com.mpgame.database.mysql.model.game.Players;
import com.mpgame.database.mysql.utils.MyBatisUtil;
import com.mpgame.utils.Log;
import org.apache.ibatis.session.SqlSession;

public class PlayersService {
    
    public int createPlayer(Players player) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PlayersMapper mapper = sqlSession.getMapper(PlayersMapper.class);
            int res = mapper.insert(player);
            sqlSession.commit();
            
            if (res > 0) {
                Log.info("创建玩家成功: " + player.getName());
                return player.getId();
            } else {
                Log.err("创建玩家失败: " + player.getName());
                return 0;
            }
        } catch (Exception e) {
            Log.err("创建玩家异常: " + e.getMessage());
            throw new RuntimeException("创建玩家异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public Players getPlayerById(int id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PlayersMapper mapper = sqlSession.getMapper(PlayersMapper.class);
            return mapper.selectById(id);
        } catch (Exception e) {
            Log.err("获取玩家失败: " + e.getMessage());
            throw new RuntimeException("获取玩家异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public Players getPlayerByAccountId(int accountId) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PlayersMapper mapper = sqlSession.getMapper(PlayersMapper.class);
            return mapper.selectByAccountId(accountId);
        } catch (Exception e) {
            Log.err("根据账号ID获取玩家失败: " + e.getMessage());
            throw new RuntimeException("根据账号ID获取玩家异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public Players getPlayerByName(String name) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PlayersMapper mapper = sqlSession.getMapper(PlayersMapper.class);
            return mapper.selectByName(name);
        } catch (Exception e) {
            Log.err("根据名称获取玩家失败: " + e.getMessage());
            throw new RuntimeException("根据名称获取玩家异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public boolean updatePlayer(Players player) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PlayersMapper mapper = sqlSession.getMapper(PlayersMapper.class);
            int res = mapper.updateById(player);
            sqlSession.commit();
            return res > 0;
        } catch (Exception e) {
            Log.err("更新玩家失败: " + e.getMessage());
            throw new RuntimeException("更新玩家异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public boolean updatePlayerCoin(int playerId, int coin) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            PlayersMapper mapper = sqlSession.getMapper(PlayersMapper.class);
            int res = mapper.updateCoin(playerId, coin);
            sqlSession.commit();
            return res > 0;
        } catch (Exception e) {
            Log.err("更新玩家金币失败: " + e.getMessage());
            throw new RuntimeException("更新玩家金币异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
